package com.efuture.servlet.simplelogindemo.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtils {

    private static DataSource ds;

    static {
       try{
           Properties properties = new Properties();
           InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties");
           properties.load(is);
           ds = DruidDataSourceFactory.createDataSource(properties);
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    //获取数据源
    public static DataSource getDataSource(){
        return ds;
    }
    //获取数据库连接对象
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    //关闭数据库连接资源
    public static void close(ResultSet rst,Connection conn,Statement stmt){
        if(null!=rst){
            try {
                rst.close();
                rst = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=conn){
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(null!=stmt){
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }


}
